define(function () {
  var jx2 = function () {
         $.get('./data/home.json',function(responseText,responseState,responseObj){
           //console.log(responseText[0].data.cart_ids);
           var shopcar = responseText[0].data.cart_ids;
           var str = '';
           for(var i in shopcar){
            // console.log(shopcar[i])
             var app_mimg = shopcar[i].app_mimg;
             var name = shopcar[i].name;
             var d_price = shopcar[i].d_price;
             var number = shopcar[i].number;
             str += `<div class="sc1">
                         <div class="sca1">
                           <span></span>
                         </div>
                         <div class="sca2">
                           <img src=`+app_mimg+` alt="">
                         </div>
                         <div class="sca3">
                           <p>`+name+`</p>
                           <div>
                             <p class='sj'>￥`+d_price+`</p> <em><span class="jian"><img src="./shopcar/img/a4.jpg" alt=""></span><span>`+number+`</span><span class="jia"><img src="./shopcar/img/a5.jpg" alt=""></span></em>
                           </div>
                         </div>
                    </div>`
           }
           $('.sc').html(str);
           var $geshu = $('.jian').next();
           var arr = [];
           for(var i =0;i<$geshu.length;i++){
             var geshu = parseInt($geshu[i].innerHTML);
               arr.push(geshu);
           }
           //console.log(arr);
           //console.log($('em').prev())
           var $jiage = $('em').prev();
           var arr1 = [];
           for(var i =0;i<$jiage.length;i++){
             var jiage = $jiage[i].innerHTML;
               var xjg = parseFloat(jiage.substring(1,jiage.length));
               arr1.push(xjg);
               // console.log(jiage.length);
           }
           //console.log(arr1);
           var gj1 =0;
           //console.log($('.sca1 span'));
           $('.sca1 span').click(function(){
                str = `<img src="./shopcar/img/a2.jpg" alt="">`

                var ht = $(this).html();
                //var count = $(this).index();
                var sj = $(this).parents('.sc1').children('.sca3').children('div').children('p').html();
                //console.log(sj);
                var xsj = parseFloat(sj.substring(1,sj.length));
                var gs = $(this).parents('.sc1').children('.sca3').children('div').children('em').children()[1].innerHTML;
                var xgs = parseInt(gs);
                // console.log(xsj);

                if(ht == ''){
                  $(this).html(str);
                    gj1 += xsj*xgs;
                   $('.jzje').html('￥'+gj1.toFixed(1));
                }else {
                  $(this).html('');
                    gj1 += -xgs*xsj;
                    //console.log(gj);
                  $('.jzje').html('￥'+gj1.toFixed(1));
                }

               for(var i =0;i<$('.sca1 span').length;i++){
                   if($('.sca1 span')[i].innerHTML == ''){
                     $('.jz1 span').html('');
                   }
               }
               if($('.sca1 span')[0].innerHTML != '' && $('.sca1 span')[1].innerHTML != '' && $('.sca1 span')[2].innerHTML != ''){
                     $('.jz1 span').html(str);
               }
           });
           $('.jz1 span').click(function(){
             var gj =0;
             str = `<img src="./shopcar/img/a2.jpg" alt="">`
                var ht = $(this).html();
                if(ht == ''){
                  $(this).html(str);
                  $('.sca1 span').html(str);
                  for(var i in arr){
                    gj += arr[i]*arr1[i];
                    gj1 = gj;
                  }
                  $('.jzje').html('￥'+gj.toFixed(1));
                  $('.jz3').html('选好了').css({
                    backgroundColor:'yellow'
                  })
                }else {
                  $(this).html('');
                  $('.sca1 span').html('');
                  $('.jzje').html('￥0');
                  $('.jz3').html('满￥0元起送').css({
                    backgroundColor:'#ccc'
                  })
                }
           });
          //console.log($('.jian'));
          $('.jian').click(function(){
            var con = parseInt($(this).next().html());
            if(con <=0){
              $(this).next().html(0);

            }else {
              $(this).next().html(con-1);


            }
          });

          $('.jia').click(function(){
            var con =parseInt($(this).prev().html());
            $(this).prev().html(con+1);
            // var index = $(this).index();
            //   gj += (arr1[index]);
            //   $('.jzje').html('￥'+parseInt(gj));
          });
         })
  }
  return {
      jx2 : jx2
  };

})
